export default {
  initLogic(list) {
    let target = []
    for (let i = 0; i < list.length; i++) {
      if (list[i].labs && list[i].labs.length) {
        target[i] = {
          logicalOperation: list[i].logicTyp,
          tagList: []
        }
        for (let j = 0; j < list[i].labs.length; j++) {
          if (list[i].labs[j].labs && list[i].labs[j].labs.length) {
            target[i].tagList[j] = {
              logicalOperation: list[i].labs[j].logicTyp,
              tagList: []
            }
            for (let k = 0; k < list[i].labs[j].labs.length; k++) {
              target[i].tagList[j].tagList.push({
                tableNm: list[i].labs[j].labs[k].labTbl,
                fieldNm: list[i].labs[j].labs[k].labFldMap,
                valueType: list[i].labs[j].labs[k].valueType,
                datTyp: list[i].labs[j].labs[k].datTyp,
                operationType: list[i].labs[j].labs[k].oprator,
                operationValue: list[i].labs[j].labs[k].valArray.length
                  ? list[i].labs[j].labs[k].valArray
                  : list[i].labs[j].labs[k].valLft
                  ? [list[i].labs[j].labs[k].valLft, list[i].labs[j].labs[k].valRit]
                  : list[i].labs[j].labs[k].val
              })
            }
          } else {
            target[i].tagList[j] = {
              tableNm: list[i].labs[j].labTbl,
              fieldNm: list[i].labs[j].labFldMap,
              valueType: list[i].labs[j].valueType,
              datTyp: list[i].labs[j].datTyp,
              operationType: list[i].labs[j].oprator,
              operationValue: list[i].labs[j].valArray.length
                ? list[i].labs[j].valArray
                : list[i].labs[j].valLft
                ? [list[i].labs[j].valLft, list[i].labs[j].valRit]
                : list[i].labs[j].val
            }
          }
        }
      } else {
        target[i] = {
          tableNm: list[i].labTbl,
          fieldNm: list[i].labFldMap,
          datTyp: list[i].datTyp,
          valueType: list[i].valueType,
          operationType: list[i].oprator,
          operationValue: list[i].valArray.length
            ? list[i].valArray
            : list[i].valLft
            ? [list[i].valLft, list[i].valRit]
            : list[i].val
        }
      }
    }
    return target
  },
  initParams(formData) {
    let derivedTag = {}
    let tagList = formData.tagList
    if (tagList.length) {
      derivedTag = {
        request: {
          labGrps: {
            dataDate: '',
            labs: [],
            level: 1,
            logicTyp: formData.logicalOperation || 'and'
          },
          grpFlter: []
        },
        extra: {},
        cache: {}
      }
      for (let i = 0; i < tagList.length; i++) {
        if (tagList[i].tagList) {
          derivedTag.request.labGrps.labs[i] = {
            labs: [],
            level: 2,
            logicTyp: tagList[i].logicalOperation
          }
          for (let j = 0; j < tagList[i].tagList.length; j++) {
            if (tagList[i].tagList[j].tagList) {
              derivedTag.request.labGrps.labs[i].labs[j] = {
                labs: [],
                level: 3,
                logicTyp: tagList[i].tagList[j].logicalOperation
              }
              for (let k = 0; k < tagList[i].tagList[j].tagList.length; k++) {
                var target = tagList[i].tagList[j].tagList[k]
                let item = {
                  labTbl: target.tableNm,
                  oprator: target.operationType,
                  datTyp: target.datTyp,
                  logicTyp: 'and',
                  labFldMap: target.fieldNm,
                  labId: target.fieldNm,
                  valueType: target.valueType
                }
                if (target.valueType == '1') {
                  item.val = ''
                  item.valLft = ''
                  item.valRit = ''
                  item.valArray = target.operationValue
                } else if (
                  target.valueType == '4' ||
                  target.valueType == '5' ||
                  target.valueType == '6' ||
                  target.valueType == '7'
                ) {
                  if (target.operationType === 'a<=x<b') {
                    item.val = ''
                    item.valLft = target.operationValue[0]
                    item.valRit = target.operationValue[1]
                    item.valArray = []
                  } else {
                    item.val = target.operationValue
                    item.valLft = ''
                    item.valRit = ''
                    item.valArray = []
                  }
                } else {
                  item.val = target.operationValue
                  item.valLft = ''
                  item.valRit = ''
                  item.valArray = []
                }
                derivedTag.request.labGrps.labs[i].labs[j].labs[k] = item
                if (!Object.keys(derivedTag.cache).find(label => label === target.fieldNm)) {
                  derivedTag.cache[target.fieldNm] = {
                    tagNm: target.fieldNm
                  }
                }
              }
            } else {
              var target2 = tagList[i].tagList[j]
              let item = {
                labTbl: target2.tableNm,
                oprator: target2.operationType,
                datTyp: target2.datTyp,
                logicTyp: 'and',
                labFldMap: target2.fieldNm,
                labId: target2.fieldNm,
                valueType: target2.valueType
              }
              if (target2.valueType == '1') {
                item.val = ''
                item.valLft = ''
                item.valRit = ''
                item.valArray = target2.operationValue
              } else if (
                target2.valueType == '4' ||
                target2.valueType == '5' ||
                target2.valueType == '6' ||
                target2.valueType == '7'
              ) {
                if (target2.operationType === 'a<=x<b') {
                  item.val = ''
                  item.valLft = target2.operationValue[0]
                  item.valRit = target2.operationValue[1]
                  item.valArray = []
                } else {
                  item.val = target2.operationValue
                  item.valLft = ''
                  item.valRit = ''
                  item.valArray = []
                }
              } else {
                item.val = target2.operationValue
                item.valLft = ''
                item.valRit = ''
                item.valArray = []
              }
              derivedTag.request.labGrps.labs[i].labs[j] = item
              if (!Object.keys(derivedTag.cache).find(label => label === target2.fieldNm)) {
                derivedTag.cache[target2.fieldNm] = {
                  tagNm: target2.fieldNm
                }
              }
            }
          }
        } else {
          let item = {
            labTbl: tagList[i].tableNm,
            oprator: tagList[i].operationType,
            datTyp: tagList[i].datTyp,
            logicTyp: 'and',
            labFldMap: tagList[i].fieldNm,
            labId: tagList[i].fieldNm,
            valueType: tagList[i].valueType
          }
          if (tagList[i].valueType == '1') {
            item.val = ''
            item.valLft = ''
            item.valRit = ''
            item.valArray = tagList[i].operationValue
          } else if (
            tagList[i].valueType == '4' ||
            tagList[i].valueType == '5' ||
            tagList[i].valueType == '6' ||
            tagList[i].valueType == '7'
          ) {
            if (tagList[i].operationType === 'a<=x<b') {
              item.val = ''
              item.valLft = tagList[i].operationValue[0]
              item.valRit = tagList[i].operationValue[1]
              item.valArray = []
            } else {
              item.val = tagList[i].operationValue
              item.valLft = ''
              item.valRit = ''
              item.valArray = []
            }
          } else {
            item.val = tagList[i].operationValue
            item.valLft = ''
            item.valRit = ''
            item.valArray = []
          }
          derivedTag.request.labGrps.labs[i] = item
          if (!Object.keys(derivedTag.cache).find(label => label === tagList[i].fieldNm)) {
            derivedTag.cache[tagList[i].fieldNm] = {
              tagNm: tagList[i].fieldNm
            }
          }
        }
      }
    }
    return derivedTag
  }
}
